<!-- Method is too OO-oriented. Methods are member-functions. Methods are not
    that popular in vocabulary outside Java or Brazil communities.  -->

Our HelloWorld project is based on testing a function that returns the sum of two numbers. 

In `sum_test.c`, we need to setup CUnit and initialize it.
```c
#include "CUnit/Basic.h"

int main() {
   // Initialize the CUnit test registry
   if (CUE_SUCCESS != CU_initialize_registry())
      return CU_get_error();

   // Sets the basic run mode, CU_BRM_VERBOSE will show maximum output of run details
   // Other choices are: CU_BRM_SILENT and CU_BRM_NORMAL
   CU_basic_set_mode(CU_BRM_VERBOSE);

   // Run the tests and show the run summary
   CU_basic_run_tests();
   return CU_get_error();
}
```
You can check if everything is alright running
```
$ gcc -o sum_test sum_test.c -lcunit
$ ./sum_test

     CUnit - A unit testing framework for C - Version 2.1-2
     http://cunit.sourceforge.net/



Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      0      0    n/a      0        0
               tests      0      0      0      0        0
             asserts      0      0      0      0      n/a

Elapsed time =    0.000 seconds
```
We are going to have only one suite and one test case which will check if we are getting the right sum[1]. 
```c
void sum_test() {
  // Check if first param matches with second[2]
  CU_ASSERT_EQUAL(sum(2, 14), 16);
  CU_ASSERT_EQUAL(sum(0, 0), 0);
  CU_ASSERT_EQUAL(sum(-1 ,6), 5);
}
```
But to be able to run this, it's necessary to add that tests into a suite in `main()`.
```c
CU_pSuite pSuite = NULL;

// Add a suite to the registry
pSuite = CU_add_suite("sum_test_suite", 0, 0);

// Always check if add was successful
if (NULL == pSuite) {
  CU_cleanup_registry();
  return CU_get_error();
}

// Add the test to the suite
if (NULL == CU_add_test(pSuite, "sum_test", sum_test)) {
  CU_cleanup_registry();
  return CU_get_error();
}
```
If you try to compile this, it'll fail, because we don't have a method `sum()`, but don't worry because that's how TDD works[3]. Just to make sure that everything is ok, you can comment the lines that contain `sum()` and check the output of run summary.

<!-- Eu mudaria o trecho seguinte, pois não tenho confiança que realmente está
seguindo estruturas comuns da língua inglesa (não lembro de ter visto algo
assim, aí eu usaria algo diferente, tipo "it's the time/moment when...").

Também, acho desnecessário avisar "crie arquivo X" e depois "preenchar arquivo
X". Acho que ficaria melhor algo como "fill a new `sum.c` file with the
following content:"-->

Now it's when you'll really start to code. First, create a file `sum.c` and then write your `sum()`.
```c
int sum(int a, int b){
  return a + b;
}
```
Remember to add `#include "sum.c"` in `sum_test.c` and run your tests again.
```bash
$ ./sum_test

     CUnit - A unit testing framework for C - Version 2.1-2
     http://cunit.sourceforge.net/


Suite: sum_test_suite
  Test: sum_test ...passed

Run Summary:    Type  Total    Ran Passed Failed Inactive
              suites      1      1    n/a      0        0
               tests      1      1      1      0        0
             asserts      3      3      3      0      n/a

Elapsed time =    0.000 seconds
```
Using TDD your flow will always look like `write test - watch it fail - make it pass`, but don't try to make all the test cases on beginning, TDD uses baby steps[4]. You can check how this test case it'll look in the end [here](https://github.com/marinalimeira/tdd-with-cunit/tree/master/sum).

[1] You can search about "[happy path](https://en.wikipedia.org/wiki/Happy_path)" and "sad path" on TDD if you want to get a better coverage.
[2] Get more assertions on [CUnit Docs](http://cunit.sourceforge.net/doc/writing_tests.html#tests)
[3] [Test-driven development cycle](https://en.wikipedia.org/wiki/Test-driven_development#Test-driven_development_cycle)
[4] [How baby are your baby-steps in TDD?](http://programmers.stackexchange.com/a/109998)
